﻿
Partial Class WebForm_ManageGroup_OpenGroup
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.InitData()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Me.BindGV()
        End If
    End Sub
    Protected Sub gv_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0
        For i As Integer = 0 To Me.gv.Rows.Count - 1
            Dim cb As CheckBox = DirectCast(Me.gv.Rows(i).FindControl("chkSelect"), CheckBox)
            Dim GroupValue As String = DirectCast(Me.gv.Rows(i).FindControl("lblGroupItem"), Label).Text.Trim()
            If cb.Checked = True Then

                sql.AppendFormat("Insert Into GroupControlSet(GroupID,GroupValue,CreateBy,CreateDate) ")
                sql.AppendFormat("Select {0} ", GroupID)
                sql.AppendFormat(",'{0}' ", GroupValue)
                sql.AppendFormat(",'{0}' ", clsAuth.GetUserID())
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat("where not exists (Select * from GroupControlSet ")
                sql.AppendFormat("Where GroupID='{0}' And GroupValue='{1}'); ", GroupID, GroupValue)

            Else
                sql.AppendFormat("Delete From GroupControlSet ")
                sql.AppendFormat("Where GroupID={0} ", GroupID)
                sql.AppendFormat("And GroupValue='{0}'; ", GroupValue)
            End If
        Next

        Try
            dat.AccData(sql)
            MyObj.ShowMessage("Apply Successful!")
        Catch ex As Exception
            'MyObj.WriteLog(sql.ToString())
            MyObj.ShowMessage("Apply Failed!")
        End Try
    End Sub

    Public Sub BindGV()
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

  

 
        sql.Length = 0
        sql.AppendFormat("select distinct v.Division,{0} as GroupValue,v.{1} as GroupItem ", IIf(Dimension = "Signature", "'('+v.Signature+')'+space(1)+p." & GroupValue, "v." & GroupValue), GroupItem)
        sql.AppendFormat(",case when IsNull(gs.GroupValue,'0')='0' then '0' else '1' end as IsCheck ")
        sql.AppendFormat("from ChannelView v ")
        If Dimension = "Signature" Then
            sql.AppendFormat("INNER JOIN ProductHierarchy p ON v.Signature=p.Signature ")
        End If
        sql.AppendFormat("left join GroupControl g on v.Division=g.Division and Dimension='{0}' ", GroupItem)
        sql.AppendFormat("left join GroupControlSet gs on gs.GroupID=g.GroupID and v.{0}=gs.GroupValue ", GroupItem)
        sql.AppendFormat("where v.Division='{0}' ", Division)
        sql.AppendFormat("and g.GroupID={0} ", GroupID)

        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()
    End Sub
    Public Sub InitData()
        Select Case Dimension
            Case "Channel"
                GroupValue = "ChannelName"
                GroupItem = "Channel"
                Title = "Customer Group / Channel"
                gv.Columns(1).HeaderText = "Customer Group / Channel"
            Case "Signature"
                GroupValue = "Signature_Text"
                GroupItem = "Signature"
                Title = "Product Hierarchy / Signature"
                gv.Columns(1).HeaderText = GroupItem
        End Select

    End Sub
    Public ReadOnly Property Division() As String
        Get
            Return Me.Request.QueryString("Division")
        End Get
    End Property
    Public ReadOnly Property GroupID() As Integer
        Get
            Return Int32.Parse(Me.Request.QueryString("GroupID"))
        End Get
    End Property


    Public ReadOnly Property Dimension() As String
        Get
            Return Me.Request.QueryString("Dimension")
        End Get
    End Property
    Public Property GroupValue() As String
        Get
            Return ViewState("GroupValue")
        End Get
        Set(ByVal value As String)
            ViewState("GroupValue") = value
        End Set
    End Property
    Public Property GroupItem() As String
        Get
            Return ViewState("GroupItem")
        End Get
        Set(ByVal value As String)
            ViewState("GroupItem") = value
        End Set
    End Property

End Class
